package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.jpa.model.Order;
import com.mxpio.erp.common.order.service.OrderLineService;
import com.mxpio.erp.common.order.service.OrderService;
import com.mxpio.erp.common.quality.entity.ScrapOutApply;
import com.mxpio.erp.common.quality.entity.ScrapOutApplyLine;
import com.mxpio.erp.common.quality.service.ScrapOutApplyLineService;
import com.mxpio.erp.common.quality.service.ScrapOutApplyService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ScrapOutApplyController", description = "报废出库申请接口")
@RestController
@RequestMapping("/erp/quality/soa/")
public class ScrapOutApplyController {

    @Autowired
    private ScrapOutApplyService scrapOutApplyService;

    @Autowired
    private ScrapOutApplyLineService scrapOutApplyLineService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderLineService orderLineService;

    @GetMapping("list")
    @Operation(summary = "报废出库申请列表", description = "获取报废出库申请列表（无分页）", method = "GET")
    public Result<List<ScrapOutApply>> list(@Parameter(description = "查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ScrapOutApply> orders = scrapOutApplyService.list(ScrapOutApply.class, criteria);
        return Result.OK(orders);
    }

    @GetMapping("page")
    @Operation(summary = "报废出库申请列表", description = "获取报废出库申请列表（分页）", method = "GET")
    public Result<Page<ScrapOutApply>> page(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<ScrapOutApply> pageOrder = scrapOutApplyService.listPage(ScrapOutApply.class, page, criteria);
        return Result.OK(pageOrder);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取报废出库申请", description = "根据bizNo获取报废出库申请", method = "GET")
    public Result<ScrapOutApply> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        ScrapOutApply scrapOutApply = scrapOutApplyService.getById(ScrapOutApply.class, bizNo);
        return Result.OK(scrapOutApply);
    }

    /*@PostMapping("add")
    @Operation(summary = "新增报废出库申请", description = "新增报废出库申请", method = "POST")
    public Result<ScrapOutApply> add(@RequestBody ScrapOutApply scrapOutApply) {
        scrapOutApplyService.save(scrapOutApply);
        return Result.OK(scrapOutApply);
    }*/

    @RequestMapping("save")
    @Operation(summary = "保存报废出库申请", description = "保存报废出库申请")
    public Result<ScrapOutApply> save(@RequestBody ScrapOutApply scrapOutApply) {
        scrapOutApplyService.save(scrapOutApply,new SmartCrudPolicyAdapter(){
        	@Override
        	public boolean beforeInsert(CrudContext context) {
        		Object o = context.getEntity();
        		if(o instanceof ScrapOutApplyLine){
        			ScrapOutApply scrapOutApply = context.getParent();
                    ((ScrapOutApplyLine) o).setBizNo(scrapOutApply.getBizNo());
        		}
        		return true;
        	}
        });
        return Result.OK(scrapOutApply);
    }

    /*@PutMapping("edit")
    @Operation(summary = "编辑报废出库申请", description = "编辑报废出库申请", method = "PUT")
    public Result<ScrapOutApply> edit(@RequestBody ScrapOutApply scrapOutApply) {
        scrapOutApplyService.update(scrapOutApply);
        return Result.OK(scrapOutApply);
    }*/

    @DeleteMapping("remove/{bizNo}")
    @Operation(summary = "根据bizNo删除报废出库申请", description = "根据bizNo删除报废出库申请,多个bizNo用英文逗号分隔", method = "DELETE")
    public Result<ScrapOutApply> remove(@PathVariable(name = "bizNo", required = true) String bizNos) {
        String[] bizNo = bizNos.split(",");
        for (String key : bizNo) {
            Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, key);
            scrapOutApplyLineService.removeBatch(ScrapOutApplyLine.class, c);
            scrapOutApplyService.remove(ScrapOutApply.class, key);
        }
        return Result.OK();
    }

    @PutMapping("open/{bizNo}")
    @Operation(summary = "打开报废出库申请", description = "打开报废出库申请", method = "PUT")
    public Result<? extends Order> open(@Parameter(description = "业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        ScrapOutApply po = scrapOutApplyService.getById(ScrapOutApply.class, bizNo);
        return orderService.updateCloseStatus(po, OrderStatusEnums.CloseStatus.OPEN);
    }

    @PutMapping("close/{bizNo}")
    @Operation(summary = "关闭报废出库申请", description = "关闭报废出库申请", method = "PUT")
    public Result<? extends Order> close(@Parameter(description = "业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        ScrapOutApply po = scrapOutApplyService.getById(ScrapOutApply.class, bizNo);
        return orderService.updateCloseStatus(po, OrderStatusEnums.CloseStatus.CLOSED);
    }

    @PutMapping("submit/{bizNo}")
    @Operation(summary = "提交报废出库申请", description = "提交报废出库申请", method = "PUT")
    public Result<? extends Order> submit(@Parameter(description = "业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        ScrapOutApply po = scrapOutApplyService.getById(ScrapOutApply.class, bizNo);
        if (po.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode())) {
            return orderService.updateBpmnStatus(po, OrderStatusEnums.BpmnStatus.AUDITING);
        } else {
            return Result.error("非开立状态不可提交");
        }
    }

    @PutMapping("agree/{bizNo}")
    @Operation(summary = "同意报废出库申请", description = "同意报废出库申请", method = "PUT")
    public Result<? extends Order> agree(@Parameter(description = "业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        return scrapOutApplyService.agreeByBizNo(bizNo);
    }

    @PutMapping("disagree/{bizNo}")
    @Operation(summary = "拒绝报废出库申请", description = "拒绝报废出库申请", method = "PUT")
    public Result<? extends Order> disagree(@Parameter(description = "业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        ScrapOutApply po = scrapOutApplyService.getById(ScrapOutApply.class, bizNo);
        if (po.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode())) {
            return orderService.updateBpmnStatus(po, OrderStatusEnums.BpmnStatus.CLOSED);
        } else {
            return Result.error("非审核中状态不可拒绝");
        }
    }

    /*@PutMapping("abandon/{bizNo}")
    @Operation(summary = "弃审报废出库申请", description = "弃审报废出库申请", method = "PUT")
    public Result<? extends Order> abandon(@Parameter(description = "业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        ScrapOutApply po = scrapOutApplyService.getById(ScrapOutApply.class, bizNo);
        if (po.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode()) || po.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode())) {
            return orderService.updateBpmnStatus(po, OrderStatusEnums.BpmnStatus.CREATE);
        } else {
            return Result.error("非审核中、已审核状态不可审核");
        }
    }*/
}


